﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_ApplyDrawing : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataBindUserInfo();

            DataBindDataGrid();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/ApplyDrawing.aspx";

        base.OnInit(e);
    }

    #endregion

    private void DataBindUserInfo()
    {
        tbUserName.Text = _User.Name;
        tbUserName.ReadOnly = true;
        tbBalacen.Text = _User.Balance.ToString("N");
        labFreeze.Text = "冻结：" + _User.Freeze.ToString("N") + " 元 ";
        labMoney.Text = "可提取金额：" + _User.Balance.ToString("N") + " 元 ";

        tbMoney.Text = PublicFunction.GetOptionsAsString("会员最低提现金额");

        //DataTable dt = DAL.Tables.T_UserDistills.Select("[ID]", "Datediff(mm, [DateTime], getdate()) = 0 and Result != -1", "");

        //if (dt == null)
        //{
        //    PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

        //    return;
        //}

        //if (dt.Rows.Count > 0)
        //{
        //    labFreeze.Visible = false;
        //    labMoney.Visible = false;
        //    tbMoney.Enabled = false;
        //    btnSubmit.Enabled = false;
        //}
    }

    private void DataBindDataGrid()
    {
        DataTable dt = DAL.Views.V_UserDistills.Select("", "[Type] = 3 and UserID = " +_User.ID , "[DateTime] desc");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string userName = this.tbUserName.Text;
        double balance = ShoveEL._Convert.StrToDouble(tbBalacen.Text, 0);
        double Money = ShoveEL._Convert.StrToDouble(tbMoney.Text, 0);
        double MinMoney = ShoveEL._Convert.StrToDouble(PublicFunction.GetOptionsAsString("会员最低提现金额"), 0);

        if (Money.Equals(0))
        {
            ShoveEL._Web.JavaScript.Alert(this, "提款金额无效!!!");
            tbMoney.Focus();

            return;
        }

        if (Money < MinMoney)
        {
            ShoveEL._Web.JavaScript.Alert(this, "提款金额不能小于最低提现金额!!!");
            tbMoney.Focus();

            return;
        }

        if (balance < Money)
        {
            ShoveEL._Web.JavaScript.Alert(this, "提款金额不能大于您的余额!!!");
            tbMoney.Focus();

            return;
        }

        int Result = 0;
        string ReturnDescription = "";

        if (DAL.Procedures.P_UserDistillAdd.Call(_User.ID, Money,_User.BankTypeName,_User.BankCardNumber, "提款",ref Result, ref ReturnDescription) < 0)
        {            
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }
        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this,ReturnDescription);            

            return;
        }


        ReturnDescription = "";
        if (_User.GetUserInfoByID(ref ReturnDescription) < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, ReturnDescription);

            return;
        }

        tbMoney.Text = string.Empty;
        tbBalacen.Text =_User.Balance.ToString();
        labFreeze.Text =_User.Freeze.ToString("N");

        DataBindDataGrid();

        ShoveEL._Web.JavaScript.Alert(this.Page, "申请提款成功!!");
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            double Money = ShoveEL._Convert.StrToDouble(e.Item.Cells[2].Text, 0);
            e.Item.Cells[2].Text = Money.ToString("N");

            //1 表示处理中 2 表示处理成功
            int Result = ShoveEL._Convert.StrToInt(e.Item.Cells[4].Text.ToString(), 0);

            if (Result == 2)
            {
                e.Item.Cells[4].Text = "已成功";
            }
            else if (Result == 1)
            {
                e.Item.Cells[4].Text = "处理中";
            }
            else if (Result == -1)
            {
                e.Item.Cells[4].Text = "拒绝提款";
            }
            else
            {
                e.Item.Cells[4].Text = "未处理";
            }

            int num = (gPager.PageIndex * gPager.PageSize) + e.Item.ItemIndex + 1;
            e.Item.Cells[0].Text = num.ToString();
        }
    }

    protected void gPager_SortBefore(object source, DataGridSortCommandEventArgs e)
    {
        DataBindDataGrid();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        DataBindDataGrid();
    }
}